using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using SamasDBI;
using SamasDBI.MySQL;
using SamasUtilities;

namespace EQEmuMysql
{
    public class Petitions
    {
        private string sCharName;
        private string sAccountName;
        private string sLastGM;
        private string sPetitionText;
        private string sGMText;
        private string sZone;
        private string sUrgency;
        private string sCharClass;
        private string sCharRace;
        private string sCharLevel;
        private string sCheckOuts;
        private string sUnavailables;
        private string sIsCheckedOut;
        private string sSentTime;
        private int nID;
        private bool flgIsNew = false;
        private bool flgDeleted = false;

        private static int nMAXID = 0;

        public string CharName
        {
            get { return sCharName; }
            set { sCharName = value; }
        }
        public string AccountName
        {
            get { return sAccountName; }
            set { sAccountName = value; }
        }
        public string LastGM
        {
            get { return sLastGM; }
            set { sLastGM = value; }
        }
        public string PetitionText
        {
            get { return sPetitionText; }
            set { sPetitionText = value; }
        }
        public string GMText
        {
            get { return sGMText; }
            set { sGMText = value; }
        }
        public string Zone
        {
            get { return sZone; }
            set { sZone = value; }
        }
        public string Urgency
        {
            get { return sUrgency; }
            set { sUrgency = value; }
        }
        public string CharClass
        {
            get { return sCharClass; }
            set { sCharClass = value; }
        }
        public string CharRace
        {
            get { return sCharRace; }
            set { sCharRace = value; }
        }
        public string CharLevel
        {
            get { return sCharLevel; }
            set { sCharLevel = value; }
        }
        public string CheckOuts
        {
            get { return sCheckOuts; }
            set { sCheckOuts = value; }
        }
        public string Unavailables
        {
            get { return sUnavailables; }
            set { sUnavailables = value; }
        }
        public string IsCheckedOut
        {
            get { return sIsCheckedOut; }
            set { sIsCheckedOut = value; }
        }
        public string SentTime
        {
            get { return sSentTime; }
            set { sSentTime = value; }
        }
        public int ID
        {
            get { return nID; }
            set { nID = value; }
        }

        public bool IsNew
        {
            get { return flgIsNew; }
        }


        public Petitions()
        {
            if (nMAXID == 0)
                nMAXID = GetMaxID();
            nMAXID++;

        sCharName = "";
        sAccountName = "";
        sLastGM = "";
        sPetitionText = "";
        sGMText = "";
        sZone = "";
        sUrgency = "";
        sCharClass = "";
        sCharRace = "";
        sCharLevel = "";
        sCheckOuts = "";
        sUnavailables = "";
        sIsCheckedOut = "";
        sSentTime = "";
            nID = nMAXID;

            flgIsNew = true;
        }

        public Petitions(int ID)
        {
            MySqlConnection conGet = new MySqlConnection(MySQLRunner.ConnectionString);
            MySqlDataReader drGet = null;
            string sSQL = "";

            sSQL = "SELECT * FROM petitions WHERE petid=" + ID.ToString();

            if (MySQLRunner.ExecuteQuery(sSQL, conGet, ref drGet) == true)
            {
                drGet.Read();

                if (drGet.HasRows == false)
                {
                    if (nMAXID == 0)
                        nMAXID = GetMaxID();

                    nMAXID++;

                    sCharName = "";
                    sAccountName = "";
                    sLastGM = "";
                    sPetitionText = "";
                    sGMText = "";
                    sZone = "";
                    sUrgency = "";
                    sCharClass = "";
                    sCharRace = "";
                    sCharLevel = "";
                    sCheckOuts = "";
                    sUnavailables = "";
                    sIsCheckedOut = "";
                    sSentTime = "";
                    nID = 0;

                    flgIsNew = true;
                }
                else
                {
                    sCharName = drGet.GetString(drGet.GetOrdinal("charname"));
                    sAccountName = drGet.GetString(drGet.GetOrdinal("accountname"));
                    sLastGM = drGet.GetString(drGet.GetOrdinal("lastgm"));
                    sPetitionText = drGet.GetString(drGet.GetOrdinal("petitiontext"));
                    sGMText = drGet.GetString(drGet.GetOrdinal("gmtext"));
                    sZone = drGet.GetString(drGet.GetOrdinal("zone"));
                    sUrgency = drGet.GetString(drGet.GetOrdinal("urgency"));
                    sCharClass = drGet.GetString(drGet.GetOrdinal("charclass"));
                    sCharRace = drGet.GetString(drGet.GetOrdinal("charrace"));
                    sCharLevel = drGet.GetString(drGet.GetOrdinal("charlevel"));
                    sCheckOuts = drGet.GetString(drGet.GetOrdinal("checkouts"));
                    sUnavailables = drGet.GetString(drGet.GetOrdinal("unavailables"));
                    sIsCheckedOut = drGet.GetString(drGet.GetOrdinal("ischeckedout"));
                    sSentTime = drGet.GetString(drGet.GetOrdinal("senttime"));
                    
                    nID = ID;

                    flgIsNew = false;
                }
            }
            else
            {
                MessageBox.Show("Error connecting to the database.");
            }

            if (conGet.State == ConnectionState.Open)
            {
                if (drGet != null)
                    drGet.Close();

                conGet.Close();
            }
        }

        public void Delete()
        {
            flgDeleted = true;
        }

        public bool Save()
        {
            bool flgReturn;

            if (flgDeleted == false)
            {
                if (flgIsNew == true)
                    flgReturn = Add();
                else
                    flgReturn = Update();
            }
            else
            {
                flgReturn = DeleteObject();
            }

            if (flgReturn == true)
                flgIsNew = false;

            return flgReturn;
        }

        private bool Update()
        {
            MySqlConnection conUpdate = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "UPDATE petitions SET " +
                   "charname='" + sCharName + "', " +
                   "accountname='" + sAccountName + "', " +
                   "lastgm='" + sLastGM + "', " +
                   "petitiontext='" + sPetitionText + "', " +
                   "gmtext='" + sGMText + "', " +
                   "zone='" + sZone + "', " +
                   "urgency='" + sUrgency + "', " +
                   "charclass='" + sCharClass + "', " +
                   "charrace='" + sCharRace + "', " +
                   "charlevel='" + sCharLevel + "', " +
                   "checkouts='" + sCheckOuts + "', " +
                   "unavailables='" + sUnavailables + "', " +
                   "ischeckedout='" + sIsCheckedOut + "', " +
                   "senttime='" + sSentTime + "' " +
                   "WHERE petid=" + nID.ToString();

            if (MySQLRunner.ExecuteNonQuery(sSQL, conUpdate) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conUpdate.State == ConnectionState.Open)
            {
                conUpdate.Close();
            }
            return flgReturn;
        }

        private bool Add()
        {
            MySqlConnection conAdd = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "";

            if (MySQLRunner.ExecuteNonQuery(sSQL, conAdd) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conAdd.State == ConnectionState.Open)
            {
                conAdd.Close();
            }

            return flgReturn;
        }

        public bool DeleteObject()
        {
            MySqlConnection conDelete = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "DELETE FROM petitions WHERE petid=" + nID.ToString();

            if (MySQLRunner.ExecuteNonQuery(sSQL, conDelete) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conDelete.State == ConnectionState.Open)
            {
                conDelete.Close();
            }

            return flgReturn;
        }

        private int GetMaxID()
        {
            MySqlConnection conGet = new MySqlConnection(MySQLRunner.ConnectionString);
            MySqlDataReader drGet = null;
            string sSQL = "";
            int nMax = 0;

            sSQL = "SELECT MAX(petid) AS ID FROM petitions";

            if (MySQLRunner.ExecuteQuery(sSQL, conGet, ref drGet) == true)
            {
                drGet.Read();

                if (drGet.HasRows == false)
                {
                    nMax = 0;
                }
                else
                {
                    nMax = drGet.GetInt32(drGet.GetOrdinal("ID"));
                }
            }
            else
            {
                MessageBox.Show("Error connecting to the database.");
            }

            if (conGet.State == ConnectionState.Open)
            {
                if (drGet != null)
                    drGet.Close();

                conGet.Close();
            }

            return nMax;
        }
    }
}
